package cn.iocoder.yudao.module.ems.controller.admin.spares;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.ems.dal.dataobject.store.StoreDO;
import cn.iocoder.yudao.module.ems.service.store.StoreService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ems.controller.admin.spares.vo.*;
import cn.iocoder.yudao.module.ems.dal.dataobject.spares.SparesDO;
import cn.iocoder.yudao.module.ems.convert.spares.SparesConvert;
import cn.iocoder.yudao.module.ems.service.spares.SparesService;

@Api(tags = "设备管理 - 备品备件")
@RestController
@RequestMapping("/ems/spares")
@Validated
public class SparesController {

    @Resource
    private SparesService sparesService;
    
    @Resource
    private StoreService storeService;

    @PostMapping("/create")
    @ApiOperation("创建备品备件")
    @PreAuthorize("@ss.hasPermission('ems:spares:create')")
    public CommonResult<Long> createSpares(@Valid @RequestBody SparesCreateReqVO createReqVO) {
        return success(sparesService.createSpares(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新备品备件")
    @PreAuthorize("@ss.hasPermission('ems:spares:update')")
    public CommonResult<Boolean> updateSpares(@Valid @RequestBody SparesUpdateReqVO updateReqVO) {
        sparesService.updateSpares(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除备品备件")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:spares:delete')")
    public CommonResult<Boolean> deleteSpares(@RequestParam("id") Long id) {
        sparesService.deleteSpares(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得备品备件")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:spares:query')")
    public CommonResult<SparesRespVO> getSpares(@RequestParam("id") Long id) {
        SparesDO spares = sparesService.getSpares(id);
        return success(SparesConvert.INSTANCE.convert(spares));
    }

    @GetMapping("/list")
    @ApiOperation("获得备品备件列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('ems:spares:query')")
    public CommonResult<List<SparesRespVO>> getSparesList(@RequestParam("ids") Collection<Long> ids) {
        List<SparesDO> list = sparesService.getSparesList(ids);
        return success(SparesConvert.INSTANCE.convertList(list));
    }

//    @GetMapping("/page")
//    @ApiOperation("获得备品备件分页")
//    @PreAuthorize("@ss.hasPermission('ems:spares:query')")
//    public CommonResult<PageResult<SparesRespVO>> getSparesPage(@Valid SparesPageReqVO pageVO) {
//        PageResult<SparesDO> pageResult = sparesService.getSparesPage(pageVO);
//        return success(SparesConvert.INSTANCE.convertPage(pageResult));
//    }

    @GetMapping("/page")
    @ApiOperation("获得备品备件分页列表")
    @PreAuthorize("@ss.hasPermission('system:spares:list')")
    public CommonResult<PageResult<SparesRespVO>> getSparesPage(@Valid SparesPageReqVO reqVO) {
        // 获得分页列表
        PageResult<SparesDO> pageResult = sparesService.getSparesPage(reqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal())); // 返回空
        }

        // 获得拼接需要的数据
        Collection<Long> storeIds = convertList(pageResult.getList(), SparesDO::getStoreId);
        Map<Long, StoreDO> storeMap = storeService.getStoreMap(storeIds);
        // 拼接结果返回
        List<SparesRespVO> sparesList = new ArrayList<>(pageResult.getList().size());
        pageResult.getList().forEach(spares -> {
            SparesRespVO respVO = SparesConvert.INSTANCE.convert(spares);
            respVO.setStore(SparesConvert.INSTANCE.convert(storeMap.get(spares.getStoreId())));
            sparesList.add(respVO);
        });
        return success(new PageResult<>(sparesList, pageResult.getTotal()));
    }


    @GetMapping("/export-excel")
    @ApiOperation("导出备品备件 Excel")
    @PreAuthorize("@ss.hasPermission('ems:spares:export')")
    @OperateLog(type = EXPORT)
    public void exportSparesExcel(@Valid SparesExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SparesDO> list = sparesService.getSparesList(exportReqVO);
        // 导出 Excel
        List<SparesExcelVO> datas = SparesConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "备品备件.xls", "数据", SparesExcelVO.class, datas);
    }

}
